#导入模块-----------------------
from urllib.request import Request,urlopen
from urllib.parse import urlencode,quote

#拼接URL地址-------------------
ur1 = "https://www.baidu.com/s?wd={}" #想要搜索的内容
word = input("请输入想要搜索的内容：")
params = quote(word)
full_url = ur1.format(params)
# print(full_url)


#向URL发送请求------------------
headers = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
request = Request(full_url,headers=headers)
#获取响应对象
resp = urlopen(request)
#获取响应内容
html = resp.read().decode("utf-8")


#保存为本地文件-----------------
filename = word +".html"
with open(filename,'w',encoding='utf-8') as f:
    f.write(html)